#include <iostream>
#include <fstream>
#include <string>
using namespace std;
FILE* fp;
unsigned char c;
unsigned int zf = 0, dc = 0, jz = 0;
bool input_m(const char* path)
{
	if ((fp = fopen(path, "rb")) != NULL)
	{
		while ((c = fgetc(fp)) != EOF)
		{
			if (((c <= 'z') && (c >= 'a')) || ((c <= 'Z') && (c >= 'A')))
			{
				zf++;
				dc++;
				while ((c = fgetc(fp)) != EOF)
				{
					if (((c <= 'z') && (c >= 'a')) || ((c <= 'Z') && (c >= 'A')))
					{
						zf++;
					}
					else if ((c == '\n')||(c == '.')||(c == '!')||(c == '?'))
					{
						jz++;
						break;
					}
					else if (c == ' ')
					{
						break;
					}
					else
					{
						break;
					}
				}
			}
			else if (c == ' ')
			{
				dc++;
			}
			else if ((c == '\n')||(c == '.')||(c == '!')||(c == '?'))
			{
				jz++;
			}
			else
			{
				break;
			}
		}
		return 1;
	}
	else
	{
		cout << "无法打开\n提示：输入的文件得是全英文txt\n输入格式为：文件名 m\n" << endl;
		return 0;
	}
}  
int main(void)
  {
       string line="";
       ifstream ifs;
      ifs.open("ruanji.txt");
      if (!ifs)
      {
          cout<<"文件打开失败"<<endl;
        exit(0);
    }
      int bSyh = 0, bXgx = 0, bHs = -1, bCode = 0, bZs = 0;
    int i,nKh = 0, nZs = 0, nDm = 0, nGg = 0, nHs = 0;
      while (!ifs.eof())
     {
          i = 0;
          getline(ifs,line);    
          bCode = 0;            
          bZs = 0;              
         if (bXgx)             
              bZs = 1;          
          while (line[i] == ' ' || line[i] == '\t' || line[i] == '\r' || line[i] == '\n')
          {
              ++i;
          }
          if (!bXgx && line[i] == '\0')  
          {
              ++nKh;
              continue;
          }
          while (1)
          {
              if (!bSyh && line[i] == '\"' && ((i > 0 && line[i-1] != '\\') || (i == 0))) 
              {
                  ++i;
                  bSyh = 1;
                  continue;
              }
              if (bSyh)
              {
                  if (line[i] == '\"' && ((i > 0 && line[i-1] != '\\') || (i == 0)))
                  {
                      bSyh = 0;
                  }
                  else if (line[i] == '\0')  
                  {
                      if (bZs)        
                          ++nZs;       
                      if (bCode)
                          ++nDm;      
                     if (bZs && bCode)
                          ++nGg;
                     break;
                 }
                  ++i;
                  continue;
              }
             if (line[i] == '\'' && ((i > 0 && line[i-1] != '\\') || (i == 0)))
             {
                  i += 3;
                 continue;
              }
              if (!bXgx && line[i] == '/' && line[i+1] == '/')  
              {
                  if (bCode)     
                 {
                 ++nZs;     
                 ++nDm;     
                 ++nGg;     
                  }
                 else          
                 { 
                      ++nZs;
                  }
                 break;  
             }
             if (!bXgx && line[i] == '/' && line[i+1] == '*')
              {
                  i += 2;        
                bXgx = 1;      
                 bZs = 1;       
               continue;
            }             
             if (bXgx)
             {
                 if (line[i] == '*' && line[i+1] == '/')
                 {
                     ++i;     
                    bXgx = 0;
                 }
                 else if (line[i] == '\0')  
                 {
                     if (bCode)       
                     {
                         ++nDm;
                        ++nZs;       
                         ++nGg;
                     }
                     else
                     {
                         ++nZs;       
                     }
                     break;
                 }
                 ++i;
                 continue;
             }
             if (line[i] == '\0')
             {
                 if (bZs)        
                     ++nZs;       
                 if (bCode)
                     ++nDm;      
                 if (bZs && bCode)
                     ++nGg;
                 break;
             }
            if (line[i] == '{')      
             {
                ++bHs;
            }
             else if (line[i] == '}') 
            {
                 if (bHs == 0)        
                    ++nHs;
                 --bHs;
             }
             
            ++i;
             bCode = 1;  
        }
     }
    char path[1000], m; 
	cout << "请输入：wc.exe -";
    cin >> m;
    cin >> path;
	input_m(path);
	if (m == 'd')
	{
		cout<<"注释： "<<nZs<<endl;
	}
	else if (m == 'e')
	{
		 cout<<"代码： "<<nDm<<endl;
	}
	else if (m == 'f')
	{
		cout<<"空行： "<<nKh<<endl;
	}
    	else if (m == 'g')
	{
		cout<<"公共： "<<nGg<<endl;
	} 
    	else if (m == 'h')
	{
		cout<<"函数： "<<nHs<<endl;
	}
     return 0;
}